// TextLineDetector.h
#ifndef TEXTLINEDETECTOR_H
#define TEXTLINEDETECTOR_H

#include "BlankScreenTypes.h"
#include <vector>

class TextLineDetector {
public:
    // 算法参数配置 - 改为 public
    struct OptimizedParams {
        // 边缘检测参数（预计算版本）
        static constexpr int GRAY_THRESHOLD_SQUARED = 64;    // 8^2，避免开方
        static constexpr int EDGE_THRESHOLD_BASE = 1;
        static constexpr int EDGE_THRESHOLD_DIVISOR = 100;
        
        // 文本行分析参数
        static constexpr int MIN_CONSECUTIVE_LINES = 1;
        static constexpr int MIN_TEXT_BLOCKS = 10;            // 最少文本块数量，用于区分骨架屏和真实文本
        static constexpr int MIN_EMPTY_RATIO_PERCENT = 10;   // 0.1的百分比版本
        static constexpr int MIN_TEXT_ROW_RATIO_PERCENT = 20; // 0.2的百分比版本
        static constexpr int MIN_AVG_EDGES = 5;              // 最小平均边缘数，用于区分骨架屏和真实文本
        
        // 颜色多样性参数
        static constexpr int COLOR_BUCKET_BITS = 4;          // 16个颜色桶
        static constexpr int COLOR_BUCKET_COUNT = 16;
        static constexpr int MIN_COLOR_BUCKETS = 2;
        static constexpr int MAX_DOMINANCE_RATIO_PERCENT = 90; // 0.9的百分比版本
        
        // 采样参数
        static constexpr int MAX_SAMPLE_ROWS = 80;
        static constexpr int MAX_SAMPLE_COLS = 150;
        static constexpr int MAX_FREQUENCY_SAMPLES = 300;
        static constexpr int MIN_PIXELS_FOR_PARALLEL = 50000; // 并行化阈值
    };

    // 优化后的接口：直接使用原始数据
    static bool hasTextLines(const uint8_t* pixelData, int width, int height, int bytesPerPixel, 
                            const Rectangle& region = Rectangle{});
    static bool hasTextContentByFrequency(const uint8_t* pixelData, int width, int height, int bytesPerPixel,
                                         const Rectangle& region = Rectangle{});

private:
    // 预计算的采样信息
    struct SamplingInfo {
        int effectiveWidth;
        int effectiveHeight;
        int startX, startY;
        int rowStep, colStep;
        int sampleRows, sampleCols;
        bool useParallel;
        
        SamplingInfo(int width, int height, const Rectangle& region, bool forFrequency = false);
    };
    
    // 核心算法：直接数据访问版本
    static std::vector<int> extractEdgeProfileDirect(const uint8_t* pixelData, int width, int height, 
                                                    int bytesPerPixel, const SamplingInfo& sampling);
    static bool analyzeTextLinePatternOptimized(const std::vector<int>& edgeProfile, 
                                               const SamplingInfo& sampling);
    static bool analyzeColorFrequencyDirect(const uint8_t* pixelData, int width, int height, 
                                           int bytesPerPixel, const SamplingInfo& sampling);
    
    // 内联辅助函数
    inline static int getGrayValue(uint8_t r, uint8_t g, uint8_t b) {
        return (r + g + b) / 3; // 快速灰度计算
    }
    
    inline static int getColorBucket(uint8_t r, uint8_t g, uint8_t b) {
        int sum = (r >> OptimizedParams::COLOR_BUCKET_BITS) + 
                  (g >> OptimizedParams::COLOR_BUCKET_BITS) + 
                  (b >> OptimizedParams::COLOR_BUCKET_BITS);
        return (sum * OptimizedParams::COLOR_BUCKET_COUNT) / (15 * 3);
    }
    
    // 安全的像素访问（带边界检查）
    inline static bool getPixelSafe(const uint8_t* pixelData, int x, int y, int width, int height, int bytesPerPixel,
                                   uint8_t& r, uint8_t& g, uint8_t& b) {
        if (!pixelData || x < 0 || y < 0 || x >= width || y >= height || bytesPerPixel < 3) {
            r = g = b = 0;
            return false;
        }
        const uint8_t* ptr = pixelData + (static_cast<int64_t>(y) * width + x) * bytesPerPixel;
        r = ptr[2]; g = ptr[1]; b = ptr[0]; // BGR转RGB
        return true;
    }
    
    // 快速像素访问（不带边界检查，调用者需确保边界安全）
    inline static void getPixelDirect(const uint8_t* pixelData, int x, int y, int width, int bytesPerPixel,
                                     uint8_t& r, uint8_t& g, uint8_t& b) {
        const uint8_t* ptr = pixelData + (static_cast<int64_t>(y) * width + x) * bytesPerPixel;
        r = ptr[2]; g = ptr[1]; b = ptr[0]; // BGR转RGB
    }
};

#endif // TEXTLINEDETECTOR_H
